package com.daoyin.ssph.action;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.daoyin.ssph.bean.Translation;
import com.daoyin.ssph.service.TranslationService;

public class TranslationAction {
	private TranslationService ta;

	
	public void setTa(TranslationService ta) {
		this.ta = ta;
	}
	private String id;
	private String title;
	private String adress;
	private String number;
	private String time;
	private String type;
	private String nature;
	private String duty;
	private String content;
	private String deadtime;
	private String scope;
	
	public void setId(String id) {
		this.id = id;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setAdress(String adress) {
		this.adress = adress;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setNature(String nature) {
		this.nature = nature;
	}

	public void setDuty(String duty) {
		this.duty = duty;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setDeadtime(String deadtime) {
		this.deadtime = deadtime;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}
	
	public String add() throws UnsupportedEncodingException{
		ServletActionContext.getRequest().setCharacterEncoding("utf-8");
		Translation t = new Translation();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		time = df.format(new Date());
		t.setTitle(title);
		t.setTime(time);
		t.setAdress(adress);
		t.setDeadtime(deadtime);
		t.setDemand(content);
		t.setDuty(duty);
		t.setNumber(Integer.parseInt(number));
		t.setScope(scope);
		t.setNature(nature);
		t.setType(type);
		t.setPublish(0);
		String str = ta.add(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";			
	}
	public String update() throws UnsupportedEncodingException{
		ServletActionContext.getRequest().setCharacterEncoding("utf-8");
		Translation t = ta.query(Integer.parseInt(id));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//�������ڸ�ʽ
		time = df.format(new Date());
		t.setId(t.getId());
		t.setTitle(title);
		t.setTime(time);
		t.setAdress(adress);
		t.setDeadtime(deadtime);
		t.setDemand(content);
		t.setDuty(duty);
		t.setNumber(Integer.parseInt(number));
		t.setScope(scope);
		t.setNature(nature);
		t.setType(type);
		t.setPublish(0);
		String str = ta.update(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";			
	}

	public String show(){
		HttpServletRequest request = ServletActionContext.getRequest();

		List<Translation> list = ta.query();
		
		request.setAttribute("show", list);
		return "success";
	
	}
	public String showDetails(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Translation translation = ta.query(Integer.parseInt(id));
		request.setAttribute("trans", translation);
		return "success";
	}
	public String delete(){
		ta.delete(Integer.parseInt(id));
		return "success";
	}
	public String publish() {
		Translation t = ta.query(Integer.parseInt(id));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//�������ڸ�ʽ
		time = df.format(new Date());
		t.setId(t.getId());
		t.setTitle(t.getTitle());
		t.setTime(time);
		t.setAdress(t.getAdress());
		t.setDeadtime(t.getDeadtime());
		t.setDemand(t.getDemand());
		t.setDuty(t.getDuty());
		t.setNumber(t.getNumber());
		t.setScope(t.getScope());
		t.setNature(t.getNature());
		t.setType(t.getType());
		t.setPublish(1);
		String str = ta.update(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";			
	}
	
}
